package com.oazon.system;

import java.util.HashMap;

import com.oazon.common.Logger;
import com.oazon.system.action.NormalAction;
import com.oazon.system.action.ScreenProtectAction;
import com.oazon.system.action.ShutDownAction;
import com.oazon.system.action.StandByAcion;
import com.oazon.system.core.IActionMessage;
import com.oazon.system.core.IActionhandler;
import com.oazon.system.handler.NormalHandler;
import com.oazon.system.handler.ScreenProtectHandler;
import com.oazon.system.handler.ShutDownHandler;
import com.oazon.system.handler.StandbyHandler;

public class SysActionChannal {
	private static String TAG = "SysActionChannal";
	private static SysActionChannal channal = null;
	private Thread timerThread = null;
	private boolean isRunning = true;
	private HashMap<String, IActionhandler> handlerMap = new HashMap<String, IActionhandler>();

	private NormalHandler normalhandler = new NormalHandler();
	private ScreenProtectHandler screenProtectHandler = new ScreenProtectHandler();
	private ShutDownHandler shutDownHandler = new ShutDownHandler();
	private StandbyHandler standbyHandler = new StandbyHandler();
	private IActionhandler currentHandler = normalhandler;
	private long count = 0;
	
	private boolean isPauseAllTimer = false;

	private SysActionChannal() {

		handlerMap.put(NormalAction.class.getName(), normalhandler);
		handlerMap.put(ScreenProtectAction.class.getName(),
				screenProtectHandler);
		handlerMap.put(ShutDownAction.class.getName(), shutDownHandler);
		handlerMap.put(StandByAcion.class.getName(), standbyHandler);
	}

	public synchronized static SysActionChannal getInstance() {
		if (channal == null) {
			channal = new SysActionChannal();
			channal.startTimer();
		}
		return channal;
	}

	public boolean isPauseAllTimer() {
		return isPauseAllTimer;
	}

	public void setPauseAllTimer(boolean isPauseAllTimer) {
		this.isPauseAllTimer = isPauseAllTimer;
	}

	/**
	 * 外部主动触发调用
	 * 
	 * @param actionMessage
	 */
	public void doAction(IActionMessage actionMessage) {
		if (actionMessage instanceof NormalAction) {
			if(currentHandler == normalhandler){
				count = 0;
				return;
			}
			currentHandler = normalhandler;
		} else if (actionMessage instanceof ScreenProtectAction) {
			if(currentHandler == screenProtectHandler){
				count = 0;
				return;
			}
			currentHandler = screenProtectHandler;
		} else if (actionMessage instanceof StandByAcion) {
			if(currentHandler == standbyHandler){
				count = 0;
				return;
			}
			currentHandler = standbyHandler;
		} else if (actionMessage instanceof ShutDownAction) {
			if(currentHandler == shutDownHandler){
				count = 0;
				return;
			}
			currentHandler = shutDownHandler;
		} else {
			return;
		}

		if (currentHandler != null) {
			currentHandler.getIn(actionMessage);
			count = 0;
		}
	}

	private void startTimer() {
		isRunning = true;
		if (timerThread == null) {
			timerThread = new Thread(new TimerRunnable());
			timerThread.start();
		}
		
	}

	private void stopTimer() {
		isRunning = false;
	}

	private class TimerRunnable implements Runnable {

		@Override
		public void run() {
			while (isRunning == true) {
				while (count < currentHandler.getTimeOut()) {
					if (currentHandler.isPauseTimer() || isPauseAllTimer) {
						count = 0;
					}
					Logger.d(TAG, currentHandler.getClass().getName() + "=="
							+ count + "===" + currentHandler.getTimeOut());
					count++;
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}//end once while
				count = 0;
				if (count != Integer.MAX_VALUE) {
					currentHandler.getOut();
				}
			}
		}
	}
}
